#生成出货预定表

import openpyxl


from openpyxl.styles import Font, Alignment, Border, Side, PatternFill

from entity.shipping_table_list_entity import ShippingTableListEntity
from ut.set_save_path_config import get_sheet_address
from ut.windows_ui import show_warning


def  print_shipping_table(shipping_table_list_entity_list,date):

    # for i in range(len(shipping_table_list_entity_list)):
    #     print(shipping_table_list_entity_list[i].po)
    #     print(shipping_table_list_entity_list[i].name)
    #     print(shipping_table_list_entity_list[i].num)
    #     print(shipping_table_list_entity_list[i].fcl)
    #     print(shipping_table_list_entity_list[i].mantissa)
    #     print(shipping_table_list_entity_list[i].pcs_sum)
    #     print(shipping_table_list_entity_list[i].box_sum)
    #     print('--------------------')





    #打印预定表

    file_name=date+"丸一预定表.xlsx"
    file_path=get_sheet_address()+"\\"+file_name
    wb=openpyxl.Workbook()
    ws=wb.create_sheet("预定表")
    wb.remove(wb["Sheet"])

    #打印表头
    ws.merge_cells(start_row=1, start_column=2, end_row=1, end_column=10)
    ws.cell(row=1, column=2).value = '丸一出货预定表'
    merge_cell = ws['B1']
    merge_cell.font = Font(name='宋体', size=26)

    alignment = Alignment(horizontal='center', vertical='center')
    merge_cell.alignment = alignment

    ws.cell(row=2, column=8).value = '出货日期：'
    ws.cell(row=2, column=8).alignment = alignment
    ws.cell(row=2, column=9).value = date
    ws.cell(row=2, column=9).alignment = alignment

    # 单元格样式
    cells_font = Font(name='宋体', size=12)
    cells_border = Border(
        left=Side(style='thin'),
        right=Side(style='thin'),
        top=Side(style='thin'),
        bottom=Side(style='thin')
    )
    # 表头
    ws.cell(row=3, column=2).value = 'PO'
    ws.cell(row=3, column=3).value = '品番'
    ws.cell(row=3, column=4).value = '每箱数量'
    ws.cell(row=3, column=5).value = '整箱数'
    ws.cell(row=3, column=6).value = '尾箱数'
    ws.cell(row=3, column=7).value = '出货数量合计'
    ws.cell(row=3, column=8).value = '出货箱数合计'
    ws.cell(row=3, column=9).value = '备注'

    for i in range(len(shipping_table_list_entity_list)):
        ws.cell(row=i + 4, column=2).value = shipping_table_list_entity_list[i].po
        ws.cell(row=i + 4, column=3).value = shipping_table_list_entity_list[i].name
        ws.cell(row=i + 4, column=4).value = shipping_table_list_entity_list[i].num
        ws.cell(row=i + 4, column=5).value = shipping_table_list_entity_list[i].fcl
        ws.cell(row=i + 4, column=6).value = shipping_table_list_entity_list[i].mantissa
        ws.cell(row=i + 4, column=7).value = shipping_table_list_entity_list[i].pcs_sum
        ws.cell(row=i + 4, column=8).value = shipping_table_list_entity_list[i].box_sum



    # 设置行高
    for row_idx in range(2, len(shipping_table_list_entity_list) + 5):
        ws.row_dimensions[row_idx].height = 33.75

    # 设置列宽
    ws.column_dimensions['A'].width = 8.38
    ws.column_dimensions['B'].width = 12.5
    ws.column_dimensions['C'].width = 17.5
    ws.column_dimensions['D'].width = 10.38
    ws.column_dimensions['E'].width = 10.38
    ws.column_dimensions['F'].width = 10.38
    ws.column_dimensions['G'].width = 12.9
    ws.column_dimensions['H'].width = 12.9
    ws.column_dimensions['I'].width = 22.63

    max_row=len(shipping_table_list_entity_list)+4

    # 合计
    ws.cell(row=max_row, column=6).value = '合计：'
    ws.cell(row=max_row, column=6).font = Font(name='宋体', size=12)
    ws.cell(row=max_row, column=6).fill = PatternFill(start_color="DAEEF3", end_color="DAEEF3", fill_type="solid")
    ws.cell(row=max_row, column=6).alignment = alignment
    ws.cell(row=max_row, column=7).value = sum(
        [shipping_table_list_entity_list[i].pcs_sum for i in range(len(shipping_table_list_entity_list))])

    ws.cell(row=max_row, column=7).font = Font(name='宋体', size=12)
    ws.cell(row=max_row, column=7).fill = PatternFill(start_color="DAEEF3", end_color="DAEEF3", fill_type="solid")
    ws.cell(row=max_row, column=7).alignment = alignment
    ws.cell(row=max_row, column=8).value = sum(
        [shipping_table_list_entity_list[i].box_sum for i in range(len(shipping_table_list_entity_list))])
    ws.cell(row=max_row, column=8).font = Font(name='宋体', size=12)
    ws.cell(row=max_row, column=8).fill = PatternFill(start_color="DAEEF3", end_color="DAEEF3", fill_type="solid")
    ws.cell(row=max_row, column=8).alignment = alignment

    # 遍历赋予样式
    for row in ws.iter_rows(min_row=3, max_row=max_row, min_col=2, max_col=9):
        for cell in row:
            cell.font = cells_font
            cell.border = cells_border
            # 居中
            alignment = Alignment(horizontal='center', vertical='center')
            cell.alignment = alignment


    # 保存文件
    try:
        wb.save(file_path)
        show_warning("预定表已保存至："+ file_path)
    except:
        show_warning('保存失败,请检查金额文件是否被占用')


def shipping_table_list_entity(compute_table_list, shipping_table_list):
    # 拼凑预定表数据
    comput_map = {}
    for i in range(len(compute_table_list)):
        comput_map[compute_table_list[i].v_name] = compute_table_list[i].p_num
    shipping_table_list_entity_list = []
    for i in range(len(shipping_table_list)):
        data = ShippingTableListEntity(None, None, None, None, None, None, None)
        data.po = shipping_table_list[i].po
        data.name = shipping_table_list[i].name
        data.num = comput_map[shipping_table_list[i].name]
        data.fcl = int(shipping_table_list[i].quantity / data.num)
        mantissa = shipping_table_list[i].quantity - data.num * data.fcl
        if mantissa == 0:
            data.mantissa = str(0)
            data.box_sum = data.fcl
        else:
            data.mantissa = '1*' + str(mantissa)
            data.box_sum = data.fcl + 1
        data.pcs_sum = shipping_table_list[i].quantity
        shipping_table_list_entity_list.append(data)
        print(data.po,data.name,data.num,data.fcl,data.mantissa,data.pcs_sum,data.box_sum)
    return shipping_table_list_entity_list



